<article id="wikiArticle">
<div> </div>
<p><strong>生成器</strong>对象是由一个 <a href="Reference/Statements/function*" title="function* 这种声明方式(function关键字后跟一个星号）会定义一个生成器函数 (generator function)，它返回一个  Generator  对象。">generator function</a> 返回的,并且它符合<a href="Reference/Iteration_protocols#iterable">可迭代协议</a>和<a href="Reference/Iteration_protocols#iterator">迭代器协议</a>。</p>
<h2 id="语法">语法</h2>
<pre><code class="language-javascript">function* gen() { 
  yield 1;
  yield 2;
  yield 3;
}

let g = gen(); 
// "Generator { }"</code></pre>
<h2 id="方法">方法</h2>
<dl>
<dt><a href="Reference/Global_Objects/Generator/next" title="next() 方法返回一个包含属性 done 和 value 的对象。该方法也可以通过接受一个参数用以向生成器传值。"><code>Generator.prototype.next()</code></a></dt>
<dd>返回一个由 <a href="Reference/Operators/yield" title="yield 关键字用来暂停和恢复一个生成器函数（(function* 或遗留的生成器函数）。"><code>yield</code></a>表达式生成的值。</dd>
<dt><a href="Reference/Global_Objects/Generator/return" title="return() 方法返回给定的值并结束生成器。"><code>Generator.prototype.return()</code></a></dt>
<dd>返回给定的值并结束生成器。</dd>
<dt><a href="Reference/Global_Objects/Generator/throw" title="throw() 方法用来向生成器抛出异常，并恢复生成器的执行，返回带有 done 及 value 两个属性的对象。"><code>Generator.prototype.throw()</code></a></dt>
<dd>向生成器抛出一个错误。</dd>
</dl>
<h2 id="示例">示例</h2>
<h3 id="一个无限迭代器">一个无限迭代器</h3>
<pre><code  class="language-javascript">function* idMaker(){
    let index = 0;
    while(true)
        yield index++;
}

let gen = idMaker(); // "Generator { }"

console.log(gen.next().value); 
// 0
console.log(gen.next().value); 
// 1
console.log(gen.next().value); 
// 2
// ...</code></pre>
<h2 id="传统的生成器对象">传统的生成器对象</h2>
<p>Firefox (SpiderMonkey) 在 <a href="/en-US/docs/Web/JavaScript/New_in_JavaScript/1.7">JavaScript 1.7</a> 中也实现了一个较早版本的生成器，其中函数声明中的星号（*）不是必需的 (只需在函数体中使用<code style="font-style: normal;">yield</code> 关键字)。但是，旧式生成器已弃用。不要使用它们;他们将被删除  (<a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1083482" rel="noopener" title="FIXED: Remove SpiderMonkey support for JS1.7 legacy generators">bug 1083482</a>)。</p>
<h3 id="传统的生成器方法">传统的生成器方法</h3>
<dl>
<dt><code>Generator.prototype.next() </code><span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span></dt>
<dd>返回 <a href="Reference/Operators/yield" title="yield 关键字用来暂停和恢复一个生成器函数（(function* 或遗留的生成器函数）。"><code>yield</code></a> 表达式产生的值. 与ES2015 生成器对象的<span style="font-family: courier,andale mono,monospace;">next()方法对应</span>.</dd>
<dt><code>Generator.prototype.close()</code> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span></dt>
<dd>关闭生成器，因此执行该函数后调用<code>next()函数时将会抛出</code> <a class="new" href="Reference/StopIteration" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>StopIteration</code></a> 错误. 与ES2015 生成器对象的<span style="font-family: courier,andale mono,monospace;">return()方法对应</span>..</dd>
<dt><code>Generator.prototype.send()</code> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span></dt>
<dd>用于将值发送到生成器。 该值由 <a href="Reference/Operators/yield" title="yield 关键字用来暂停和恢复一个生成器函数（(function* 或遗留的生成器函数）。"><code>yield</code></a> 表达式返回, 并且返回下一个 <a href="Reference/Operators/yield" title="yield 关键字用来暂停和恢复一个生成器函数（(function* 或遗留的生成器函数）。"><code>yield</code></a> 表达式产生的值. <code>send(x)</code> 对应于ES2015生成器对象中的 <code>next(x)</code></dd>
<dt><strong><code>Generator.</code></strong><code>prototype.</code><strong><code>throw()</code> </strong> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span></dt>
<dd>向生成器抛出错误. 与ES2015 生成器对象的<span style="font-family: courier,andale mono,monospace;">throw()方法对应</span>.</dd>
</dl>
<h3 id="旧生成器对象示例">旧生成器对象示例</h3>
<pre><code  class="language-javascript">function fibonacci() {
  var a = yield 1;
  yield a * 2;
}

var it = fibonacci();
console.log(it);          // "Generator {  }"
console.log(it.next());   // 1
console.log(it.send(10)); // 20
console.log(it.close());  // undefined
console.log(it.next());   // throws StopIteration (as the generator is now closed)
</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-generator-objects" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Generator objects</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>Initial definition.</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-generator-objects" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">Generator objects</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p></p><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
            This compatibility table still uses the old format,
            because we haven't yet converted the data it contains.
            <strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div><p></p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>39.0</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #f00;">未实现</span></td>
<td>39.0</td>
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="See_also" name="See_also">相关链接</h2>
<h3 id="Legacy_generators">Legacy generators</h3>
<ul>
<li><a href="Reference/Statements/Legacy_generator_function" title="遗留的生成器函数语句 使用特殊参数声明遗留的生成器函数。">The legacy generator function</a></li>
<li><a href="Reference/Operators/Legacy_generator_function" title="function 关键字可以用于在表达式中定义旧式的生成器函数。为使定义的函数为一个旧式的生成器函数，该函数的函数体中需要至少包含一个 yield 表达式。">The legacy generator function expression</a></li>
<li><a class="new" href="Reference/StopIteration" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>StopIteration</code></a></li>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features/The_legacy_Iterator_protocol">The legacy Iterator protocol</a></li>
</ul>
<h3 id="ES2015_generators">ES2015 generators</h3>
<ul>
<li><a href="Reference/Functions" title="有关更多示例和说明，请参阅有关函数的JavaScript指南。">Functions</a></li>
<li><a href="Reference/Statements/function" title="函数声明定义一个具有指定参数的函数。"><code>function</code></a></li>
<li><a href="Reference/Operators/function" title="function 关键字可以用来在一个表达式中定义一个函数。"><code>function expression</code></a></li>
<li><a href="Reference/Function" title="此页面仍未被本地化, 期待您的翻译!"><code>Function</code></a></li>
<li><a href="Reference/Statements/function*" title="function* 这种声明方式(function关键字后跟一个星号）会定义一个生成器函数 (generator function)，它返回一个  Generator  对象。"><code>function*</code></a></li>
<li><a href="Reference/Operators/function*" title="function*关键字可以在表达式内部定义一个生成器函数。"><code>function* expression</code></a></li>
<li><a href="Reference/Global_Objects/GeneratorFunction" title="GeneratorFunction构造器生成新的生成器函数 对象。在JavaScript中，生成器函数实际上都是GeneratorFunction的实例对象。"><code>GeneratorFunction</code></a></li>
<li><a href="/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol">The Iterator protocol</a></li>
</ul>
</article>